<?php
namespace control;
/*
*消息中心类
*/

class messageClass extends adIndexClass
{
    public array $messageType = ['公司通知', '公司动态'];

    /**
     * 查询表单
     *
     * @param string $see
     * @return string
     */
    public function homeForm(string $see)
    {
        if ($see == '否') {
            $title = L('person.table.time').' - '.L('person.table.time');
            $type = 'date';
            $style = ' width200';
        } else {
            $title = L('person.table.seeTime').' - '.L('person.table.seeTime');
            $type = 'datetime';
            $style = ' width300';
        }
        $html =
            text(["name" => "text", "title" => L('person.table.nei_rong')]) .
            hidden(["name" => "see", 'value' => $see]) .
            text(["name" => "date", "title" => $title, 'class' => 'text' . $style, 'laydate' => ['range' => '~', 'type' => $type]]);
        return $this->search($html, 'menu');
    }


    /**
     * 待阅 - 列表页 - 查询条件
     *
     * @param string $see
     * @return string
     */
    public function home($see = '否')
    {
        if ($see == '否') {
            $data = [
                "id" => "all",
                "menu" => [
                    "pending" => L('person.table.tobeRead'),
                    "processed" => L('person.table.readed'),
                ]
            ];
        }
        $data['html'] = $this->homeForm('否');
        return $this->tab($data);
    }

    /**
     * 已阅 - 列表页 - 查询条件
     *
     */
    public function processed()
    {
        $json['html'] = $this->homeForm('是');
        return json_encode($json, 320);
    }

    public function processedSearch()
    {
        return $this->homeSearch();
    }

    /**
     * 查询结果
     *
     * @return string
     */
    public function homeSearch()
    {
        //like查询数组
        $like = array(
            "text" => $this->post('text'),//内容
        );
        //全等查询数组
        $equal = array(
            "see" => $this->post('see'),//阅读
        );
        $and = " AND stid = '".self::$stid."' ";
        if (!empty($this->post('date'))) {
            $and .= self::timeComparison($this->post('date'), 'time', ($this->post('see') == '否') ? 'seeTime' : 'time');
        }
        //获取查询结果
        $where = search(array("like" => $like, "=" => $equal)) . $and;
        $page = $this->page(array("table" => "message", "where" => $where, "order" => "time desc"));
        //获取表头
        if ($this->post('see') == '否') {
            $checkAll = $this->checkAll("listForm", "listId");
            $tr[] = array($checkAll,L('person.table.nei_rong'), L('person.table.txsj'));
        } else {
            $tr[] = array(L('person.table.nei_rong'), L('person.table.txsj'), L('person.table.cksj'));
        }
        //获取记录
        foreach ($page['array'] as $array) {
            $info = query('audit', " id ='$array[typeId]'");
            if (in_array($array['type'], $this->messageType)) {
                $array['text'] = "<span class='del-row-button' onclick=\"window.open('" . root . "control/adIndex.html?cla=notification&fun=previewPage&id={$array['typeId']}')\">{$array['text']}</span>";
            }else if (!empty($array['targetCla']) && !empty($array['targetFun']) && empty($info['id'])) {
                $array['text'] = $array['see'] == '否'?"<span class='del-row-button' onclick=\"see('{$array['type']}','{$array['targetCla']}','{$array['targetFun']}','{$array['typeId']}','{$array['id']}')\">" . $array['text'] . "</span>":"<span class='del-row-button' onclick=\"newIframe('{$array['type']}详情','".root."control/adIndex.html?cla={$array['targetCla']}&fun={$array['targetFun']}&id={$array['typeId']}')\">" . $array['text'] . "</span>";
            } else {
                $array['text'] = $array['see'] == '否'?"<span class='del-row-button' onclick=\"see('{$array['type']}','{$info['targetCla']}','{$info['targetFun']}','{$info['targetId']}','{$array['id']}')\">" . $array['text'] . "</span>":"<span class='del-row-button' onclick=\"newIframe('{$array['type']}详情','".root."control/adIndex.html?cla={$info['targetCla']}&fun={$info['targetFun']}&id={$info['targetId']}')\">" . $array['text'] . "</span>";
            }
            if ($array['see'] == '否') {
                //列表控件
                $dominate = array(
                    "box" => array("name" => "listId", "value" => $array['id']),
                );
                $tr[] = [$this->dominate($dominate), $array['text'], $array['time']];
            } else {
                $tr[] = [$array['text'], $array['time'], $array['seeTime']];
            }
        }
        //返回
        $button = '';
        if ($this->post('see') == '否') {
            $button = "<span onclick=\"layer('message','layerModel',{type:'read'})\" class='spanButton'>".L('person.table.mark')."</span>\n";
        }
        $json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'];
        $json['html'] .= <<<EOT
        <script>
        function see(type,targetCla,targetFun,targetId,messageId){
            $.post(root+'control/adIndex.html?cla=homepage&fun=see',{messageId:messageId},function(res) {
               if (res.warn ===2){
                    newIframe(type+'详情',root+'control/adIndex.html?cla='+targetCla+'&fun='+targetFun+'&id='+targetId)
               }else{
                   warn(res.warn);
               }
            },'json')
        }
        </script>
EOT;
        return json_encode($json, 320);
    }

    /**
     * 弹窗
     * @return bool|string
     */
    public function layerModel()
    {
        //准备数据
        $html = $this->layerEdit(array("table" => array(
            array(
                must . L('person.table.tip')."：" => L('person.table.markMessageDone'),
            )
        ), 'formTwo' => 'listForm'));

        return $this->layer(array("title" => L('person.table.biao_ji'), "height" => 180, "html" => $html));
    }

    /**
     *弹窗编辑
     */
    public function layerModelEdit()
    {
        $listId = $this->post('listId');
        if (empty($listId) || !is_array($listId)) {
            $json['warn'] = L('person.table.chooseMarkMessage');
        } else {
            $json['warn'] = update(array("table" => "message", "field" => array('see' => '是', 'seeTime' => $this->time), "where" => "id IN ('" . implode("','", $listId) . "') and see = '否'"));
        }
        if (in_array($json['warn'], array("新增成功", "更新成功"))) {
            //添加日志
            $text =  str_replace(['{{staffName}}'],[$this->control['name']],L('person.table.markDoneMessage'));
            $this->logText(array("type" => "message", "text" => $text));
            //返回
            $_SESSION['warn'] = L('person.table.bjcg');
            $json['warn'] = 2;
        }
        return json_encode($json, 320);
    }
}
